﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Client.ControlHelper;
using System.Collections.Generic;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Content.GuestBook {
    public partial class List : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                LoadEOList();
            }
        }

        private void LoadEOList() {
            String hql = " from VGuestbookEO eo where eo.OwnerKey ='" + base.OwnerKey + "' ";
            if (!String.IsNullOrEmpty(this.txt_Content.Text)) {
                hql += " and eo.Content like '%" + this.txt_Content.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(this.ddl_Type.SelectedValue)) {
                hql += " and eo.TypeKey = '" + this.ddl_Type.SelectedValue + "' ";
            }
            if (!String.IsNullOrEmpty(this.txt_Name.Text)) {
                hql += " and eo.Poster like '%" + this.txt_Name.Text + "%' ";
            }
            if (!String.IsNullOrEmpty(ddl_Verify.SelectedValue)) {
                hql += " and eo.IsVerified =" + this.ddl_Verify.SelectedValue;
            }
            if (this.rbl_Reply.SelectedValue == "0") {
                hql += " and (eo.Reply='' or eo.Reply is null) ";
            } else if(this.rbl_Reply.SelectedValue == "1"){
                hql += " and (eo.Reply<>'' and eo.Reply is not null) ";
            }
            hql += " order by eo.LastDate desc ";
            IList<VGuestbookEO> list = biz.Find<VGuestbookEO>(hql);
            this.gvList.DataSource = list;
            this.gvList.DataBind();
        }

        private void InitForm() {
            ICodeBiz codeBiz = BizFactory.Get<ICodeBiz>();
            this.ddl_Type.DataValueField = "CodeKey";
            this.ddl_Type.DataTextField = "CodeName";
            this.ddl_Type.DataSource = codeBiz.LoadCodeByType(CodeTypeDef.Guestbook.Key);
            this.ddl_Type.DataBind();
            this.ddl_Type.Items.Insert(0, new ListItem(TxtDef.DDLTextAll, ""));
        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            LoadEOList();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvList.PageIndex = e.NewPageIndex;
            LoadEOList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            String eoKey = e.CommandArgument.ToString();
            if (!base.Ac.IsLogined) return;
            if (e.CommandName == "Reply") {//回复
                Response.Redirect("Reply.aspx?id=" + eoKey);
            } else if (e.CommandName == "Del") {
                biz.Delete<TGuestbookEO>(eoKey);
                this.LoadEOList();
            } else if (e.CommandName == "Verify") {
                try {
                    TGuestbookEO eo = biz.Load<TGuestbookEO>(eoKey);
                    eo.IsVerified = 1;
                    eo.VerifyDate = DateTime.Now;
                    eo.VerifyOperator = base.Actor.ID;
                    biz.Update<TGuestbookEO>(eo);
                    SiteInfo.SendSucceed(Page, "");
                } catch(Exception ex) {
                    log.Error(ex.Message);
                    SiteInfo.SendFailed(Page, "");
                }
                this.LoadEOList();
            }
        }
    }
}
